package com.project.lk.nurseapp.login;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;

import com.hujiang.permissiondispatcher.NeedPermission;
import com.jcodecraeer.xrecyclerview.SimpleViewSwitcher;
import com.project.lk.nurseapp.R;
import com.project.lk.nurseapp.base.BaseApp;
import com.project.lk.nurseapp.base.UrlContants;
import com.project.lk.nurseapp.db.UserDaoBean;
import com.project.lk.nurseapp.db.UserDaoBeanDao;
import com.project.lk.nurseapp.home.MainActivity;
import com.project.lk.nurseapp.login.model.UserBean;
import com.project.lk.nurseapp.login.presenter.ILoginView;
import com.project.lk.nurseapp.login.presenter.LoginPresenter;
import com.project.lk.nurseapp.nurse.NurseActivity;
import com.project.lk.nurseapp.utils.AppManager;
import com.project.lk.nurseapp.utils.ProgressUtils;
import com.project.lk.nurseapp.utils.ToastUtils;
import com.project.lk.nurseapp.utils.UserInfoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
/**
 * 功能介绍：
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/9
 * 修改日期：
 */
@NeedPermission(permissions = {Manifest.permission.RECORD_AUDIO,
        Manifest.permission.CAMERA,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
        , Manifest.permission.ACCESS_FINE_LOCATION,
        Manifest.permission.ACCESS_COARSE_LOCATION})
public class LoginActivity extends AppCompatActivity implements ILoginView<UserBean> {
    @BindView(R.id.login_user)
    EditText loginUser;
    @BindView(R.id.login_psw)
    EditText loginPsw;
    private Unbinder mBind;
    @BindView(R.id.mProgressBar)
    SimpleViewSwitcher mProgressBar;
    private LoginPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(getLayoutId());
        //设置全屏
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        getWindow().addFlags(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        getWindow().addFlags(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        AppManager.getAppManager().addActivity(this);
        //绑定黄油刀
        mBind = ButterKnife.bind(this);
        init();

    }

    private void init() {
        initData();
        initView();
    }

    protected void initData() {
        //查询本地
        if (new UserInfoUtils().getUsername() != null) {
            startActivity(new Intent(this, NurseActivity.class)
                    .putExtra("login", "login"));
            AppManager.getAppManager().finishActivity(this);
            return;
        }
        presenter = new LoginPresenter(this);
    }

    protected void initView() {
        ProgressUtils.setProgress(mProgressBar);
    }

    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    String logintype = "";
    String openid = "";
    String nickname = "";
    String headimg = "";

    @OnClick({R.id.wx_login_but, R.id.out_login_but, R.id.login_but, R.id.register_but})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.wx_login_but: //微信登录
//                if (mProgressBar.getVisibility()== View.GONE) {
//
//                    if (TextUtils.isEmpty(loginUser.getText())) {
//                        ToastUtils.showShortToast("手机号不为空");
//                    } else if (TextUtils.isEmpty(loginPsw.getText())) {
//                        ToastUtils.showShortToast("密码不为空");
//                    } else {
//                        mProgressBar.setVisibility(View.VISIBLE);
//                        presenter.postLogin(UrlContants.LOGIN_URL, loginUser.getText().toString()
//                                , loginPsw.getText().toString());
//                    }
//                }else {
//                    ToastUtils.showShortToast("正在登录中。。。");
//                }
//                startActivity(new Intent(this, MainActivity.class));

                ToastUtils.showShortToast("微信暂时无法登录");
                break;
            case R.id.out_login_but: //离线登录
                if (mProgressBar.getVisibility() == View.GONE) {
                    startActivity(new Intent(this, NurseActivity.class)
                            .putExtra("login", "out"));
                } else {
                    ToastUtils.showShortToast("登录中，请勿离线登录");
                }

                break;
            case R.id.login_but: //登录
                if (mProgressBar.getVisibility() == View.GONE) {
                    if (TextUtils.isEmpty(loginUser.getText())) {
                        ToastUtils.showShortToast("手机号不为空");
                    } else if (TextUtils.isEmpty(loginPsw.getText())) {
                        ToastUtils.showShortToast("密码不为空");
                    } else {
                        mProgressBar.setVisibility(View.VISIBLE);
                        presenter.postLogin(UrlContants.LOGIN_URL, loginUser.getText().toString()
                                , loginPsw.getText().toString(), "0", openid, nickname, headimg);
                    }
                } else {
                    ToastUtils.showShortToast("正在登录中。。。");
                }

                break;
            case R.id.register_but: //注册
                startActivity(new Intent(this, RegisterActivity.class));
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.dettachView();
            presenter = null;
        }
        if (mBind != null) {
            mBind.unbind();
        }
    }


    @Override
    public void loginSuccess(List<UserBean> list) {
        if (list != null && list.size() != 0) {
            UserBean loginBean = list.get(0);
            UserDaoBeanDao userDao = BaseApp.getInstance().getDaoSession().getUserDaoBeanDao();
            UserDaoBean user = UserInfoUtils.getUserInfoFromCache(String.valueOf(1));
            if (user == null) {
                user = new UserDaoBean();
            }
            user.setAccountId(loginBean.getAccountId());
            if (loginBean.getNickName() != null) {
                user.setNickName(loginBean.getNickName());
            }
            if (loginBean.getBirth() != null) {
                user.setBirth(loginBean.getBirth());
            }

            if (loginBean.getHeadImg() != null) {
                user.setHeadImg(loginBean.getHeadImg());
            }
            if (loginBean.getMobile() != null) {
                user.setMobile(loginBean.getMobile());
            }
            if (loginBean.getBaseUserInfoId() != null) {
                user.setBaseUserInfoId(loginBean.getBaseUserInfoId());
            }
            if (loginBean.getUsername() != null) {
                user.setUsername(loginBean.getUsername());
            }
            if (loginBean.getSex() != null) {
                user.setSex(loginBean.getSex());
            }
            if (loginBean.getHealthUrl()!=null){
                user.setHealthUrl(loginBean.getHealthUrl());
            }
            userDao.save(user);

            startActivity(new Intent(LoginActivity.this, NurseActivity.class)
                    .putExtra("login", "login"));
            AppManager.getAppManager().finishActivity();
            mProgressBar.setVisibility(View.GONE);
        } else {
            ToastUtils.showShortToast("登录异常");
        }


    }

    @Override
    public void loginFail() {
        mProgressBar.setVisibility(View.GONE);
    }

}
